package com.wulin;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.io.*;
import java.net.Socket;

public class Client extends JFrame {
    private JPanel contentPane;
    private JTextField textField;
    private JTextArea textArea;
    private Socket client;
    private DataOutputStream dos;
    private DataInputStream dis;

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {

                Client frame = new Client();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public Client() throws IOException {
        setTitle("客户端");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 826, 710);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(117, 95, 349, 47);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("发送");
        btnNewButton.addActionListener(e -> {
            String mes = textField.getText();
            try {
                if (dos != null) {
                    dos.writeUTF(mes);
                    dos.flush();
                    textArea.append("发送: " + mes + "\n");
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        btnNewButton.setBounds(543, 110, 153, 37);
        contentPane.add(btnNewButton);

        textArea = new JTextArea();
        textArea.setBounds(117, 243, 377, 329);
        contentPane.add(textArea);

        // 启动客户端并处理连接
        new Thread(() -> {
            try {
//                BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
//                System.out.println("请输入用户名");
//                String uname = console.readLine();
//                System.out.println("请输入密码");
//                String upwd = console.readLine();
                client = new Socket("localhost", 8860);
                dos = new DataOutputStream(client.getOutputStream());
                dos.writeUTF("已连接");
//                dos.writeUTF("uname=" + uname + "&upwd=" + upwd);
//                System.out.println("uname=" + uname + "&upwd=" + upwd);
                dos.flush();
                dis = new DataInputStream(client.getInputStream());
                String s = dis.readUTF();
                System.out.println(s);

                // 启动接收消息线程


                new Thread(() -> {
                    try {
                        while (true) {
                            String receivedMes = dis.readUTF();
                            textArea.append("接收: " + receivedMes + "\n");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

